package com.ruoyi.store.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.service.IWarehouseService;
import com.ruoyi.system.api.domain.store.Warehouse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 仓库信息Controller
 *
 * @author wl
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/warehouseInfo")
@Slf4j
public class WarehouseController extends BaseController {
    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 查询仓库信息列表
     */
    @RequiresPermissions("store:warehouseInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(Warehouse warehouse) {
        log.info("WarehouseController list warehouse:{}", warehouse);
        TableDataInfo tableDataInfo = getDataTable(warehouseService.selectWarehouseList(warehouse));
        log.info("WarehouseController list tableDataInfo:{} warehouse:{}", tableDataInfo, warehouse);
        return tableDataInfo;
    }

    /**
     * 获取仓库信息列表
     */
    @RequiresPermissions("store:warehouseInfo:list")
    @GetMapping("/list/all")
    public AjaxResult listAll() {
        log.info("WarehouseController listAll start ...");
        AjaxResult ajaxResult = warehouseService.listAll();
        log.info("WarehouseController listAll end ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 导出仓库信息列表
     */
    @RequiresPermissions("store:warehouseInfo:export")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Warehouse warehouse) {
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        util.exportExcel(response, list, "仓库信息数据");
    }

    /**
     * 获取仓库信息详细信息
     */
    @RequiresPermissions("store:warehouseInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("WarehouseController getInfo id:{}", id);
        AjaxResult ajaxResult = warehouseService.selectWarehouseById(id);
        log.info("WarehouseController getInfo ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 新增仓库信息
     */
    @RequiresPermissions("store:warehouseInfo:add")
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Warehouse warehouse) {
        log.info("WarehouseController add warehouse:{}", warehouse);
        AjaxResult ajaxResult = warehouseService.insertWarehouse(warehouse);
        log.info("WarehouseController add ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 修改仓库信息
     */
    @RequiresPermissions("store:warehouseInfo:edit")
    @Log(title = "仓库信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Warehouse warehouse) {
        log.info("WarehouseController edit warehouse:{}", warehouse);
        AjaxResult ajaxResult = warehouseService.updateWarehouse(warehouse);
        log.info("WarehouseController edit ajaxResult:{} warehouse:{}", ajaxResult, warehouse);
        return ajaxResult;
    }

    /**
     * 删除仓库信息
     */
    @RequiresPermissions("store:warehouseInfo:remove")
    @Log(title = "仓库信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("WarehouseController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = warehouseService.deleteWarehouseByIds(ids);
        return ajaxResult;
    }

    /**
     * @return AJaxResult
     * @description 校验仓库编码
     * @date 2024/10/26 上午10:18
     * @Param houseCode:
     **/
    @GetMapping("/checkHouseCode/{houseCode}")
    public AjaxResult checkHouseCode(@PathVariable String houseCode) {
        log.info("WarehouseController checkHouseCode houseCode:{}", houseCode);
        AjaxResult ajaxResult = warehouseService.checkHouseCode(houseCode);
        log.info("WarehouseController checkHouseCode ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }
}
